﻿using System.Collections.Generic;
using System.Text;

namespace GPLib
{
    public static class KeyboardLayout
    {

        private static string GetKeyName( byte key )
        {
            StringBuilder lpString = new StringBuilder( 20 );
            while ( WinNative.GetKeyNameText( key << 16, lpString, lpString.Capacity ) == lpString.Capacity - 1 )
                lpString.Capacity *= 2;

            return lpString.ToString();
        }

        /// <summary>
        /// Gets all the unmodified keys on the keyboard
        /// </summary>
        public static char[] GetUnmodifiedKeys
        {

            get
            {

                HashSet< char > currentKeys = new HashSet< char >();

                for ( byte i = 0; i < 254; ++i )
                {
                    string currentKey = GetKeyName( i );

                    if ( currentKey.Length == 1 )
                        currentKeys.Add( char.ToLower( currentKey[ 0 ] ) );
                }

                char[] unmodifiedkeys = new char[ currentKeys.Count ];
                currentKeys.CopyTo( unmodifiedkeys );

                return unmodifiedkeys;

            }

        }

        /// <summary>
        /// Gets the characters that require a shift, except in the cases of symbols.
        /// </summary>
        public static char[] GetShiftedKeys
        {

            get
            {
                HashSet<char> currentKeys = new HashSet<char>();

                for ( byte i = 0; i < 254; ++i )
                {
                    string currentKey = GetKeyName( i );

                    if ( currentKey.Length == 1 && char.ToLower( currentKey[ 0 ] ) != char.ToUpper( currentKey[ 0 ] ) )
                        currentKeys.Add( char.ToUpper( currentKey[ 0 ] ) );
                }

                char[] shiftedKeys = new char[ currentKeys.Count ];
                currentKeys.CopyTo( shiftedKeys );

                return shiftedKeys;
            }

        }

    }

}
